//
//  AYSCategoryDetailViewController.m
//  AiYingShiProject
//
//  Created by lanou on 15/10/28.
//  Copyright © 2015年 李晓理. All rights reserved.
//

#import "AYSCategoryDetailViewController.h"
#import "AYSCategoryTableViewCell.h"
#import "AYSRequestManager.h"
#import "AYSHomeMoreDataViewController.h"
#import "AYSHomeMoreCollectionViewCell.h"
#import "AYSCommonPlayViewController.h"
#import "SVProgressHUD.h"
@interface AYSCategoryDetailViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *mainTableView;

@property (nonatomic, strong) NSArray *movieCategoryArray;
@property (nonatomic, strong) NSArray *tvCategoryArray;
@property (nonatomic, strong) NSArray *cartonCategoryArray;
@property (nonatomic, strong) UIButton *rightBarButton;
@property (nonatomic, strong) AYSHomeMoreDataViewController *moreDataVC;
@property (nonatomic, strong) AYSCommonPlayViewController *playVC;

@property (nonatomic, strong) NSMutableDictionary *dataSource;

@end

@implementation AYSCategoryDetailViewController

- (void)didClickMoreButton:(UIBarButtonItem *)sender
{
    _moreDataVC = [[AYSHomeMoreDataViewController alloc] init];
    _moreDataVC.type = self.type;
    [self.navigationController pushViewController:_moreDataVC animated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    switch (_type)
    {
        case CategoryTypeMovie:
            self.titleLabel.text = @"电影";
            break;
        case CategoryTypeTV:
            self.titleLabel.text = @"电视剧";
            break;
        case CategoryTypeCarton:
            self.titleLabel.text = @"动漫";
            break;
        case CategoryTypeZY:
            self.titleLabel.text = @"综艺";
            break;
        default:
            self.titleLabel.text = @"其他";
            break;
    }
    
    _rightBarButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [_rightBarButton setTitle:@"更多" forState:(UIControlStateNormal)];
    _rightBarButton.frame = CGRectMake(SCREEN_WIDTH - 60, 30, 60, 30);
    _rightBarButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [_rightBarButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [_rightBarButton addTarget:self action:@selector(didClickMoreButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.topView addSubview:_rightBarButton];
    
    _movieCategoryArray = @[@"最新", @"大陆", @"香港", @"韩国", @"日本", @"法国", @"英国", @"德国", @"台湾", @"美国"];
    _tvCategoryArray = @[@"最新", @"伦理", @"喜剧", @"悬疑", @"都市", @"偶像", @"古装", @"军事", @"警匪", @"历史", @"武侠", @"科幻", @"宫廷", @"情景"];
    _cartonCategoryArray = @[@"国漫", @"日漫", @"恋爱", @"美少女", @"运动", @"校园", @"搞笑", @"幻想", @"冒险", @"悬疑", @"热血"];
    _dataSource = [NSMutableDictionary dictionaryWithCapacity:0];
    
    [self requestData];
}

- (void)createTableView
{
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:(UITableViewStylePlain)];
    [_mainTableView registerClass:[AYSCategoryTableViewCell class] forCellReuseIdentifier:@"AYSCategoryTableViewCell"];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.allowsSelection = NO;
    _mainTableView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_mainTableView];
}

- (void)requestData
{
    switch (_type)
    {
        case CategoryTypeMovie:
            [self requestMovieData];
            break;
        case CategoryTypeTV:
            [self requestTVData];
            break;
        case CategoryTypeCarton:
            [self requestCartonData];
            break;
        case CategoryTypeZY:
            break;
        default:
            break;
    }
}

- (void)requestCartonData
{
    
    for (int i = 0; i < _cartonCategoryArray.count; ++i)
    {
        [SVProgressHUD show];
        NSString *urlString = nil;
        NSString *urlStringUTF8 = nil;
        if (i == 0)
        {
            NSString *str = @"大陆";
            urlStringUTF8 = [[NSString stringWithString:str] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            urlString = [NSString stringWithFormat:@"http://doukantv.com/api/top/?pagesize=20&ver=2.3.4&sys_ver=9.1&type=katong&category=all&area=%@&pageindex=1&cli=iphone", urlStringUTF8];
        }
        else if (i == 1)
        {
            NSString *str = @"日本";
            urlStringUTF8 = [[NSString stringWithString:str] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            urlString = [NSString stringWithFormat:@"http://doukantv.com/api/top/?pagesize=20&ver=2.3.4&sys_ver=9.1&type=katong&category=all&area=%@&pageindex=1&cli=iphone", urlStringUTF8];
        }
        else
        {
            urlStringUTF8 = [_cartonCategoryArray[i] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            urlString = [NSString stringWithFormat:@"http://doukantv.com/api/top/?pagesize=20&ver=2.3.4&sys_ver=9.1&type=katong&category=%@&area=all&pageindex=1&cli=iphone", urlStringUTF8];
        }
        
        [AYSRequestManager requestWithType:GET url:urlString parDic:nil finish:^(NSData *data)
         {
             NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
             NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:0];
             for (NSDictionary *dict in dic[@"result"])
             {
                 AYSCategoryModel *model = [[AYSCategoryModel alloc] init];
                 [model setValuesForKeysWithDictionary:dict];
                 [tempArray addObject:model];
             }
             [_dataSource setObject:tempArray forKey:_cartonCategoryArray[i]];
             if (i == _cartonCategoryArray.count - 1)
             {
                 [self createTableView];
             }
             [SVProgressHUD dismiss];
             [_mainTableView reloadData];
             
         } error:^(NSError *error)
         {
         }];
    }
}

- (void)requestTVData
{
    for (int i = 0; i < _tvCategoryArray.count; ++i)
    {
        [SVProgressHUD show];
        NSString *urlString = nil;
        NSString *urlStringUTF8 = nil;
        if (i == 0)
        {
            urlString = @"http://doukantv.com/api/top/?pagesize=20&ver=2.3.4&sys_ver=9.1&type=tv&year=2015&area=all&category=all&pageindex=1&cli=iphone";
        }
        else
        {
            urlStringUTF8 = [_tvCategoryArray[i] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            urlString = [NSString stringWithFormat:@"http://doukantv.com/api/top/?pagesize=20&ver=2.3.4&sys_ver=9.1&type=tv&year=all&area=all&category=%@&pageindex=1&cli=iphone", urlStringUTF8];
        }
        
        [AYSRequestManager requestWithType:GET url:urlString parDic:nil finish:^(NSData *data)
         {
             NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
             NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:0];
             for (NSDictionary *dict in dic[@"result"])
             {
                 AYSCategoryModel *model = [[AYSCategoryModel alloc] init];
                 [model setValuesForKeysWithDictionary:dict];
                 [tempArray addObject:model];
             }
             [_dataSource setObject:tempArray forKey:_tvCategoryArray[i]];
             if (i == _tvCategoryArray.count - 1)
             {
                 [self createTableView];
             }
             [SVProgressHUD dismiss];
             [_mainTableView reloadData];
             
         } error:^(NSError *error)
         {
         }];
    }
}

- (void)requestMovieData
{
    for (int i = 0; i < _movieCategoryArray.count; ++i)
    {
        [SVProgressHUD show];
        NSString *urlString = nil;
        NSString *urlStringUTF8 = nil;
        if (i == 0)
        {
            urlString = @"http://doukantv.com/api/top/?pagesize=20&ver=2.3.4&sys_ver=9.1&type=movie&year=2015&area=all&pageindex=1&cli=iphone";
        }
        else
        {
            urlStringUTF8 = [_movieCategoryArray[i] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            urlString = [NSString stringWithFormat:@"http://doukantv.com/api/top/?pagesize=20&ver=2.3.4&sys_ver=9.1&type=movie&year=all&area=%@&pageindex=1&cli=iphone", urlStringUTF8];
        }
        
        [AYSRequestManager requestWithType:GET url:urlString parDic:nil finish:^(NSData *data)
         {
             NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
             NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:0];
             for (NSDictionary *dict in dic[@"result"])
             {
                 AYSCategoryModel *model = [[AYSCategoryModel alloc] init];
                 [model setValuesForKeysWithDictionary:dict];
                 [tempArray addObject:model];
             }
             [_dataSource setObject:tempArray forKey:_movieCategoryArray[i]];
             if (i == _movieCategoryArray.count - 1)
             {
                 [self createTableView];
             }
             [SVProgressHUD dismiss];
             [_mainTableView reloadData];
             
         } error:^(NSError *error)
         {
         }];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (_type)
    {
        case CategoryTypeMovie:
            return [_movieCategoryArray count];
        case CategoryTypeTV:
            return [_tvCategoryArray count];
        case CategoryTypeCarton:
            return [_cartonCategoryArray count];
        case CategoryTypeZY:
            break;
        default:
            break;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AYSCategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AYSCategoryTableViewCell" forIndexPath:indexPath];
    
    // Configure the cell...
    cell.backgroundColor = [UIColor blackColor];
    
    switch (_type)
    {
        case CategoryTypeMovie:
            [cell configureModelArray:_dataSource[_movieCategoryArray[indexPath.row]]];
            cell.title.text = _movieCategoryArray[indexPath.row];
            break;
        case CategoryTypeTV:
            [cell configureModelArray:_dataSource[_tvCategoryArray[indexPath.row]]];
            cell.title.text = _tvCategoryArray[indexPath.row];
            break;
        case CategoryTypeCarton:
            [cell configureModelArray:_dataSource[_cartonCategoryArray[indexPath.row]]];
            cell.title.text = _cartonCategoryArray[indexPath.row];
            break;
        default:
            break;
    }
    [cell reloadCollectionViewData];
    cell.clickItem = ^void(NSInteger item)
    {
        _playVC = [[AYSCommonPlayViewController alloc] init];
        switch (_type)
        {
            case CategoryTypeMovie:
                _playVC.programeID = [_dataSource[_movieCategoryArray[indexPath.row]][item] ID];
                break;
            case CategoryTypeTV:
                _playVC.programeID = [_dataSource[_tvCategoryArray[indexPath.row]][item] ID] ;
                break;
            case CategoryTypeCarton:
                _playVC.programeID = [_dataSource[_cartonCategoryArray[indexPath.row]][item] ID];
                break;
            default:
                break;
        }
        [self.navigationController pushViewController:_playVC animated:YES];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 220;
}

@end
